<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索添加 -->
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getProductTypeList()">
            <el-button slot="append" icon="el-icon-search" @click="getProductTypeList()"></el-button>
          </el-input>
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加产品类别</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表 -->
      <el-table :data="productTypeList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="类别名称" prop="pro_ca_name"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row.pro_ca_id)" circle></el-button>
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" @click="deleteProductType(scope.row.pro_ca_id)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>

    <!-- 创建新产品类别对话框 -->
    <el-dialog title="添加产品类别" :visible.sync="addDialogVisible" width="50%"
               @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" :rules="addFormRules"
               ref="addFormRef" label-width="70px">
        <!-- 类别名称 -->
        <el-form-item label="类别名" prop="pro_ca_name">
          <el-input v-model="addForm.pro_ca_name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addProductType">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改产品类别对话框 -->
    <el-dialog title="修改产品类别信息" :visible.sync="editDialogVisible" width="50%" @colse="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <el-form-item prop="pro_ca_id" hidden>
          <el-input v-model="editForm.pro_ca_id" hidden></el-input>
        </el-form-item>
        <!-- 用户名 -->
        <el-form-item label="类别名" prop="pro_ca_name">
          <el-input v-model="editForm.pro_ca_name"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editProductTypeInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 请求数据
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5
      },
      productTypeList: [],// 用户列表

      total: 0, // 最大数据记录
      addDialogVisible: false ,// 对话框显示
      // 添加产品类别表单项
      addForm: {
        pro_ca_id:'',
        pro_ca_name:''
      },
      // 控制修改用户对话框显示/隐藏
      editDialogVisible:false,
      // 修改用户信息
      editForm:{},
      addFormRules:{
        pro_ca_name: [
          { required: true, message: "请输入类别名称", trigger: "blur" },
          { min: 1, max: 10, message: "请输入正确的类别名称", trigger: "blur" }
        ]
      },
      editFormRules:{
        pro_ca_name: [
          { required: true, message: "请输入类别名称", trigger: "blur" },
          { min: 1, max: 10, message: "请输入正确的类别名称", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getProductTypeList();
  },
  methods: {
    async getProductTypeList() {
      // 调用post请求
      const { data: res } = await this.$http.get("productType/productTypeList", {
        params: this.queryInfo
      });
      this.productTypeList = res.data; // 将返回数据赋值
      this.total = res.numbers; // 总个数
    },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getProductTypeList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getProductTypeList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.addFormRef.resetFields();// 重置表单项
    },
    // 添加产品类别
    addProductType(){
      this.$refs.addFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("productType/addProductType",this.addForm);
        if (res !== "success") {
          userinfo.state = !userinfo.state;
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getProductTypeList();
      })
    },
    // 展示修改框
    async showEditDialog(id){

      const {data:res} = await this.$http.post("productType/getUpdate?id="+id);
      // if (res != "success") {
      // userinfo.state = !userinfo.state;
      // return this.$message.error("操作失败！！！");
      // }
      // this.$message.success("操作成功！！！");

      this.editForm = res;
      this.editDialogVisible = true;
    },
    // 关闭窗口
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
    // 确认修改
    editProductTypeInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("productType/editProductType",this.editForm);
        console.log(res);
        if (res !== "success") return this.$message.error("操作失败！！！");
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getProductTypeList();
      });
    },
    // 删除按钮
    async deleteProductType(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.post("productType/deleteProductType?id="+id);
      if (res !== "success") {
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
      this.getProductTypeList();
    },
  }
};
</script>
<style lang="less" scoped>
</style>